package com.zhuchaooasystem.ui.home.service;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.ServiceUnusualListHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.base.BaseEvent;
import com.zhuchaooasystem.entity.ServiceUnusualEntity;
import com.zhuchaooasystem.entity.UnusualDetailEntity;
import com.zhuchaooasystem.event.ServiceDetailEvent;
import com.zhuchaooasystem.presenter.home.serviceunusual.ServiceUnusualContract;
import com.zhuchaooasystem.presenter.home.serviceunusual.ServiceUnusualPresenter;
import com.zhuchaooasystem.ui.common.ImagePagerActivity;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ScreenUtils;
import com.zhuchaooasystem.utils.ToastUtil;
import com.zhuchaooasystem.widget.SlideMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class ServiceUnusualActivity extends BaseActivity<ServiceUnusualPresenter> implements ServiceUnusualContract.View {
    @BindView(R.id.actionbar_txt_title)
    TextView txt_title;
    @BindView(R.id.actionbar_iv_back)
    ImageView iv_back;
    @BindView(R.id.actionbar_iv_right)
    ImageView iv_right;
    //----
    @BindView(R.id.act_service_unusual_recycle)
    SlideMenuRecyclerView recycle;

    //----
    private boolean canEdit;
    private int projectId;
    private String orderCode;
    private List<ServiceUnusualEntity.ResultBean.SuccessfulBean> unusualList;
    private RecyclerArrayAdapter adapter;
    //-----
    private int cur_position;

    @Override
    protected int getLayout() {
        return R.layout.activity_service_unusual;
    }

    @Override
    protected ServiceUnusualPresenter getPresenter() {
        return new ServiceUnusualPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {
        if (null != getIntent() && null != getIntent().getExtras()) {
            canEdit = getIntent().getExtras().getBoolean("canEdit");
            projectId = getIntent().getExtras().getInt("projectId");
            orderCode = getIntent().getExtras().getString("orderCode");
        }
        RxBus.get().register(this);
    }


    @Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("newUnusual")})
    public void newUnusual(BaseEvent baseEvent) {
        if (null != baseEvent) {
            mPresenter.getUnusualList();
            RxBus.get().post("RedactToDetail", new ServiceDetailEvent());
        }
    }

    @Override
    protected void initView() {
        setActionBar();
        //----
        initRecycle();
    }

    private void initRecycle() {

        recycle.setLayoutManager(new LinearLayoutManager(mContext));
        DividerDecoration itemDecoration = new DividerDecoration(getResources().getColor(R.color.color_e1e1e1), ScreenUtils.dip2px(this, 1f), 0, 0);
        itemDecoration.setDrawLastItem(true);
        recycle.addItemDecoration(itemDecoration);
        recycle.setAdapter(adapter = new RecyclerArrayAdapter<ServiceUnusualEntity.ResultBean.SuccessfulBean>(mContext) {

            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ServiceUnusualListHolder(parent, ServiceUnusualActivity.this);
            }
        });
        recycle.setOnItemClickListener(new SlideMenuRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mPresenter.getUnusualDetail(unusualList.get(position).getItemId());
            }

            @Override
            public void onDeleteClick(int position) {
                cur_position=position;
                mPresenter.deleteUnusual(unusualList.get(position).getItemId());
            }
        });
    }

    private void setActionBar() {
        txt_title.setText(getString(R.string.actionbar_title_service_unusual));
        iv_back.setVisibility(View.VISIBLE);
        if (canEdit) {
            iv_right.setImageResource(R.drawable.ic_add);
            iv_right.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void initDataAndLoadData() {
        mPresenter.getUnusualList();
    }

    @Override
    public void refreshData(List<ServiceUnusualEntity.ResultBean.SuccessfulBean> data) {
        this.unusualList = data;
        adapter.clear();
        adapter.addAll(data);
    }

    public boolean isCanEdit() {
        return canEdit;
    }

    public void setCanEdit(boolean canEdit) {
        this.canEdit = canEdit;
    }

    @Override
    public int getProjectId() {
        return projectId;
    }

    @Override
    public void deleteSuccuss() {
        ToastUtil.showShort(mContext, "删除成功");
        unusualList.remove(cur_position);
        adapter.clear();
        adapter.addAll(unusualList);
        RxBus.get().post("RedactToDetail", new ServiceDetailEvent());
    }

    @Override
    public void detailSuccuss(UnusualDetailEntity.ResultBean.SuccessfulBean entity) {
        if (null != entity && null != entity.getListModel() && entity.getListModel().size() > 0) {
            Intent intent = new Intent(this, ImagePagerActivity.class);
            ArrayList<String> list_images = new ArrayList<>();
            for (int i = 0; i < entity.getListModel().size(); i++) {
                list_images.add(entity.getListModel().get(i).getFilePath());
            }
            intent.putExtra("images", list_images);
            intent.putExtra("content", entity.getTitle());
            intent.putExtra("showSave", true);
            startActivity(intent);
        } else {
            ToastUtil.showShort(this, "该异常说明没有图片信息");
        }
    }

    @OnClick({R.id.actionbar_iv_back, R.id.actionbar_iv_right})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.actionbar_iv_back:
                finish();
                break;
            //新建
            case R.id.actionbar_iv_right:
                Bundle bundle = new Bundle();
                bundle.putInt("projectId", projectId);
                bundle.putString("orderCode", orderCode);
                AppUtils.jump(mContext, ServiceNewUnusualActivity.class, bundle);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxBus.get().unregister(this);
    }

}
